<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Author: ThanhTV
* Email: thanhtran@vietnambiz.com
* Date create: Web,24/04/2011
* Description: Collection controller
*/
require_once('admin_controller'.EXT);
class product_type extends admin_controller
{
    var $data = array();
    
    function product_type()
    {
        $this->load->library('pagination');
		parent::admin_controller();
		$this->load->model(array("product_type_model"));	
    }

    function index()
    {
        $this->data["all_product_type"] = $this->product_type_model->get_all_product_type(1,20);        
        $this->data['tpl_view'] = 'product_type/index';
        $this->load->view('admin_layout',$this->data);
    }

    function list_ajax($page = 1)
    {        
        //pagination
        $limit 		= ($this->input->post('limit') > 0) ? $this->input->post('limit') : 20;
        $num_rows 	= $this->product_type_model->get_count_all_product_type();
        $total 		= ceil($num_rows/$limit);
        $url 		= base_url_admin('product_type/list_ajax');
        $this->session->set_userdata('url_list_product_type',$url . '/' . $page);
        $conf = array(
            'cur_page'      =>	$page,
            'total_rows'	=>	$total,
            'base_url'      =>  $url,
            'js_function'	=>	'list_ajax'
        );
        $this->pagination->initialize($conf);
        $this->data['pagnav'] = $this->pagination->ajax_url();
        $this->data['limit'] = $limit;
        //end pagination

        $this->data["all_product_type"] = $this->product_type_model->get_all_product_type($page,$limit);        

        $this->load->view('product_type/list_ajax',$this->data);
    }

    function add()
    {
        if($_SERVER["REQUEST_METHOD"]=="POST")
        {
            $this->form_validation->set_rules('name', 'name', 'required');
            if($this->form_validation->run() == TRUE)
            {
                $name   	= $this->input->post('name');                
                
                if($this->product_type_model->check_title($name)==FALSE)
                {                                       
                    $arr_in = array(
                        'name'                  => $name,                    	
                        'name_link'             => create_link($name)                        
                    );

                    $re = $this->db->insert('product_type', $arr_in);
                    if(!$re)
                        {die("not_ok");}
                        else{die("ok");}
                 }
                 else{
                        die("name_exits");
                     }
            }
        }
          $this->load->view('product_type/add',$this->data);
    }

    function edit()
    {
        $act 	= $this->input->post('act');
        $id 	= $this->input->post("id");
        if($act == 'process')
        {
            $name   	= $this->input->post('name');            

            if($this->product_type_model->check_title_edit($name,$id)==FALSE)
            {                               
                $arr_in = array(
                        'name'                  => $name,                		
                        'name_link'             => create_link($name)                	                        
                    );
                $re = $this->db->where('id', $id)->update('product_type', $arr_in);
                die("ok");
            }
            else{ die("name_exits"); }
        }
        $this->data["product_type_info"] = $this->product_type_model->get_product_type_info($id);
        $this->load->view('product_type/edit',$this->data);
    }
    
    function del($id)
    {
        if(!$this->product_type_model->del_product_type($id))
        {die('no');}
        die('yes');
    }

    function view($id=0)
    {
        $this->data["product_type_info"] = $this->product_type_model->get_product_type_info($id);
        $this->load->view('product_type/view',$this->data);
    }
}